package com.web.kdl.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * @author zq
 * @date 2022/7/2 20:34
 * 该工具用于解析站点的信息
 */
public class MessageUtil {

    /*
    站点1网关消息的处理类
     */
    public Map<String, Object> getSite1Message(JSONObject jsonObject){
        // 取出网关数据中的data数组
        JSONArray data = jsonObject.getJSONArray("data");
        String time = jsonObject.getString("time");
        DateUtil dateUtil = new DateUtil();
        //转换时间格式20220111142750-->2022-01-11 14:27:50
        Map<String,Object> map = dateUtil.timeGetUtil(time);
        int dataLength = data.size();
        // siteInfo 储存站点的网关数据
        JSONObject siteInfo = new JSONObject();
        int fidValue;
        int avgValue;
        for (int i=0; i<dataLength;i++){
            fidValue = data.getJSONObject(i).getIntValue("fid");
            avgValue = (int) data.getJSONObject(i).getFloatValue("avg");
            siteInfo.put(String.valueOf(fidValue),avgValue);
        }
        map.put("siteInfo",siteInfo);
        return map;
    }

    /*
    站点3网关消息的处理类
     */
    public Map<String, Object> getSite3Message(JSONObject jsonObject){
        //取出时间和飞轮编号以及数据
        String time = jsonObject.getString("time");
        String nid = jsonObject.getString("nid");
        JSONArray data = jsonObject.getJSONArray("data");
        DateUtil dateUtil = new DateUtil();
        Map<String,Object> map = dateUtil.timeGetUtil(time);
        int dataLength = data.size();
        // siteInfo 储存站点的网关数据
        JSONObject siteInfo = new JSONObject();
        int fidValue;
        int avgValue;
        for (int i=0; i<dataLength;i++){
            fidValue = data.getJSONObject(i).getIntValue("fid");
            avgValue = (int) data.getJSONObject(i).getFloatValue("avg");
            siteInfo.put(String.valueOf(fidValue),avgValue);
        }
        map.put("nid",nid);
        map.put("siteInfo",siteInfo);
        return map;
    }
    /*
    站点3网关消息，取出系统控制信息
     */
    public JSONObject getSite3SystemMessage(JSONObject jsonObject){
        //取出时间和飞轮编号以及数据
        JSONObject systemData = new JSONObject();
        String[] systemNum = new String[3];
        systemNum[0] = "40001";
        systemNum[1] = "40002";
        systemNum[1] = "40021";
        for (int i = 0; i<systemNum.length; i++){
            systemData.put(systemNum[i],jsonObject.get(systemNum[i]));
        }
        for (int i = 40010; i<=40019; i++){
            systemData.put(String.valueOf(i),jsonObject.get(String.valueOf(i)));
        }
        for (int i = 40023; i<=40033; i++){
            systemData.put(String.valueOf(i),jsonObject.get(String.valueOf(i)));
        }
        return systemData;
    }

}
